package org.agent.systeminit;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.agent.common.Constants;
import org.agent.pojo.SystemConfig;
import org.agent.service.systemconfig.SystemConfigService;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SystemInit implements ServletContextListener {
	private Logger logger = Logger.getLogger(SystemInit.class);
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		// TODO Auto-generated method stub
		/**
		 * 1.拿到ioc容器(在web项目中)：WebApplicationContext
		 * */
		System.out.println("初始化spring WebApplicationContext");
		Constants.webApplicationContext =	WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
		if(null==Constants.webApplicationContext){
			logger.info("=========初始化webApplicationContext失败=====");
		}else{
			logger.info("=========初始化webApplicationContext成功=====");
		}
		//拿到systemConfigService,
		if(Constants.webApplicationContext!=null){
			SystemConfigService configService = (SystemConfigService) Constants.webApplicationContext.getBean("systemConfigService");	
			SystemConfig systemConfig = new SystemConfig();
			systemConfig.setIsStart(1);//仅使用已启用的配置项
			Constants.systemConfigList =  configService.getSystemConfigs(systemConfig);	
			Constants.configSystemDate();
			logger.info("==============="+Constants.accountConfigList.size());
			logger.info("===systemConfigList=========="+Constants.systemConfigList.size());
			logger.info("==============="+Constants.sellConfigList.size());
			logger.info("==============="+Constants.customTypeConfigList.size());
			logger.info("==============="+Constants.serviceConfigList.size());
			logger.info("==============="+Constants.cardTypeConfigList.size());
			logger.info("=========初始化完成=====");
		}
		
		//调用service的方法
	}

}
